/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "pzcar_i2c_ctrl.h"
#include "pzcar_wheel_ctrl.h"
#include "net_config.h"

#include "ssd1306.h"

extern char* eCm_String[];
extern char* eSt_String[];
extern PzCarInfo g_carInfo;
extern char* WifiGetIP(void);

extern unsigned char page_curr;
extern unsigned char item_curr;

#define BAR  (3)

void DisplayNetInfo(unsigned char page)
{
    unsigned char pos_y;

    char ipInfo[32] = {};
    char ptInfo[8] = {};
    char ssidInfo[32] = {};
    char pswdInfo[32] = {};

    if (page == 1) {
        if (g_carInfo.e_NetST == Net_ST_Conned) { //STA
            ssd1306_Fill(Black);
            ssd1306_SetCursor(BAR, BAR);
            ssd1306_DrawString("STA Info", Font_11x18, White);   // 7x10   11x18
            
            memset(ssidInfo, 0, sizeof(ssidInfo));
            sprintf(ssidInfo, "ssid:%s", ssid);
            ssd1306_SetCursor(BAR, BAR+18+BAR);
            ssd1306_DrawString(ssidInfo, Font_7x10, White);   // 7x10   11x18

            memset(pswdInfo, 0, sizeof(pswdInfo));
            sprintf(pswdInfo, "pswd:%s", pswd);
            ssd1306_SetCursor(BAR, BAR+18+BAR+10+BAR);
            ssd1306_DrawString(pswdInfo, Font_7x10, White);   // 7x10   11x18
            ssd1306_SetCursor(60, 54);
            ssd1306_DrawString("Next Page", Font_7x10, White);   // 7x10   11x18
            ssd1306_UpdateScreen();
        } else {  // AP
            ssd1306_Fill(Black);
            ssd1306_SetCursor(BAR, BAR);
            ssd1306_DrawString("AP Info", Font_11x18, White);   // 7x10   11x18
            
            memset(ssidInfo, 0, sizeof(ssidInfo));
            sprintf(ssidInfo, "ssid:%s", AP_SSID);
            ssd1306_SetCursor(BAR, BAR+18+BAR);
            ssd1306_DrawString(ssidInfo, Font_7x10, White);   // 7x10   11x18

            memset(pswdInfo, 0, sizeof(pswdInfo));
            sprintf(pswdInfo, "pswd:%s", AP_PSWD);
            ssd1306_SetCursor(BAR, BAR+18+BAR+10+BAR);
            ssd1306_DrawString(pswdInfo, Font_7x10, White);   // 7x10   11x18
            ssd1306_SetCursor(60, 54);
            ssd1306_DrawString("Next Page", Font_7x10, White);   // 7x10   11x18
            ssd1306_UpdateScreen();
        }
    } else if (page == 2) {
        ssd1306_Fill(Black);
        ssd1306_SetCursor(BAR, BAR);
        ssd1306_DrawString("IP/Port:", Font_11x18, White);   // 7x10   11x18

        if (g_carInfo.e_NetST == Net_ST_Conned) { //STA
            memset(ipInfo, 0, sizeof(ipInfo));
            sprintf(ipInfo, "%s", WifiGetIP());
            ssd1306_SetCursor(BAR, BAR+18+BAR);
            ssd1306_DrawString(ipInfo, Font_7x10, White);   // 7x10   11x18
        } else { //AP
            memset(ipInfo, 0, sizeof(ipInfo));
            sprintf(ipInfo, "192.168.1.1");
            ssd1306_SetCursor(BAR, BAR+18+BAR);
            ssd1306_DrawString(ipInfo, Font_7x10, White);   // 7x10   11x18
        }

        pos_y = BAR+18+BAR;
        #if SERVER_UDP
        pos_y = pos_y+10+BAR;
        memset(ptInfo, 0, sizeof(ptInfo));
        sprintf(ptInfo, "UDP port:%d", UDP_SERVERPORT);
        ssd1306_SetCursor(BAR, pos_y);
        ssd1306_DrawString(ptInfo, Font_7x10, White);   // 7x10   11x18
        #endif
        #if SERVER_TCP
        pos_y = pos_y+10+BAR;
        memset(ptInfo, 0, sizeof(ptInfo));
        sprintf(ptInfo, "TCP port:%d", TCP_SERVERPORT);
        ssd1306_SetCursor(BAR, BAR+18+BAR+10+BAR);
        ssd1306_DrawString(ptInfo, Font_7x10, White);   // 7x10   11x18
        #endif
        ssd1306_SetCursor(60, 54);
        ssd1306_DrawString("Next Page", Font_7x10, White);   // 7x10   11x18
        ssd1306_UpdateScreen();
    }
}

void DisplayTask(void)
{
    unsigned char i = 0;
    unsigned char pos_y;
    char speedBuff[10] = {};
    char* Page_0_Mode[eMode_Max] = {
        "Mode:Auto",    // item_curr
        "Mode:Track",
        "Mode:AudCmd",
        "Mode:NetCtrl",
    };
    char* Page_1_NetCtrl[Net_MODE_NET_MAX] = {
        "NetServer",
        "NetConfig",
    };

    ssd1306_Init();

    while (1) {
        switch (page_curr) {
        case 0:  // display Menu[0]
            ssd1306_Fill(Black);
            pos_y = BAR;
            for (i=0; i<eMode_Max; i++) {
                if(i == item_curr) {
                    ssd1306_SetCursor(BAR, pos_y);
                    ssd1306_DrawString(Page_0_Mode[i], Font_11x18, White);
                    pos_y = pos_y + 18 + BAR;
                } else {
                    ssd1306_SetCursor(BAR, pos_y);
                    ssd1306_DrawString(Page_0_Mode[i], Font_7x10, White);
                    pos_y = pos_y + 10 + BAR;
                }
            }
            ssd1306_UpdateScreen();
            break;
        case 1:
            ssd1306_Fill(Black);
            pos_y = BAR;
            for (i=0; i<Net_MODE_NET_MAX; i++) {
                if(i == item_curr) {
                    ssd1306_SetCursor(BAR, pos_y);
                    ssd1306_DrawString(Page_1_NetCtrl[i], Font_11x18, White);
                    pos_y = pos_y + 18 + BAR;
                } else {
                    ssd1306_SetCursor(BAR, pos_y);
                    ssd1306_DrawString(Page_1_NetCtrl[i], Font_7x10, White);
                    pos_y = pos_y + 10 + BAR;
                }                
            }
            ssd1306_UpdateScreen();
            break;
        case 2:
            if ((g_carInfo.e_CM == eMode_NetCtrl) &&
                (g_carInfo.e_NetMode == Net_MODE_NET_SERVER)) {
                if (item_curr == 0) {
                    ssd1306_Fill(Black);
                    ssd1306_SetCursor(BAR, BAR);
                    ssd1306_DrawString(eCm_String[g_carInfo.e_CM], Font_11x18, White);   // 7x10   11x18
                    ssd1306_SetCursor(BAR, BAR+18+BAR);
                    ssd1306_DrawString(eSt_String[g_carInfo.e_ST], Font_7x10, White);
                    ssd1306_SetCursor(BAR, BAR+18+BAR+10+BAR);
                    memset(speedBuff, 0, sizeof(speedBuff));
                    sprintf(speedBuff, "Speed:%d", g_carInfo.d_speed_curr);
                    ssd1306_DrawString(speedBuff, Font_7x10, White);
                    ssd1306_SetCursor(60, 54);
                    ssd1306_DrawString("Next Page", Font_7x10, White);   // 7x10   11x18
                    ssd1306_UpdateScreen();
                } else {
                    DisplayNetInfo(item_curr);
                }
            } else {
                ssd1306_Fill(Black);
                ssd1306_SetCursor(BAR, BAR);
                ssd1306_DrawString(eCm_String[g_carInfo.e_CM], Font_11x18, White);   // 7x10   11x18
                ssd1306_SetCursor(BAR, BAR+18+BAR);
                ssd1306_DrawString(eSt_String[g_carInfo.e_ST], Font_7x10, White);
                ssd1306_SetCursor(BAR, BAR+18+BAR+10+BAR);
                memset(speedBuff, 0, sizeof(speedBuff));
                sprintf(speedBuff, "Speed:%d", g_carInfo.d_speed_curr);
                ssd1306_DrawString(speedBuff, Font_7x10, White);
                ssd1306_UpdateScreen();
            }
            break;
        case 3:
            if (item_curr == 0) {
                ssd1306_Fill(Black);
                ssd1306_SetCursor(BAR, BAR);
                ssd1306_DrawString("NetConfig", Font_11x18, White);   // 7x10   11x18
                
                ssd1306_SetCursor(BAR, BAR+18+BAR);
                ssd1306_DrawString("  AP/STA  Info", Font_7x10, White);   // 7x10   11x18

                ssd1306_SetCursor(BAR, BAR+18+BAR+10+BAR);
                ssd1306_DrawString("  IP/Port Info", Font_7x10, White);   // 7x10   11x18
                
                ssd1306_SetCursor(60, 54);
                ssd1306_DrawString("Next Page", Font_7x10, White);   // 7x10   11x18
                ssd1306_UpdateScreen();
            } else {
                DisplayNetInfo(item_curr);
            }
            break;
        default:
            break;
        }
        usleep(500*1000);
    }
}

osThreadId_t DisplayTaskEntry(void)
{
    //osPriorityAboveNormal[32], osPriorityNormal[24]
    //{.name, .attr_bits, .cb_mem, .cb_size, .stack_mem, .stack_size, .priority, .tz_module, .reserved}
    osThreadAttr_t attr = {"DisplayTask", 0, NULL, 0, NULL, 1024*4, 24, 0, 0};
    osThreadId_t taskId = osThreadNew(DisplayTask, NULL, &attr);

    if (taskId == NULL) {
        printf("%s: Failed to create %s!\n", __func__, attr.name);
        return NULL;
    }
    return taskId;
}
//SYS_RUN(DisplayTasktEntry);